package main

import "fmt"

func romanToInt(s string) int {
	length := len(s)
	mp := getRomanMap()
	res := 0
	for i := 0; i < length; i++ {
		cur := mp[string(s[i])]
		if i+1 < length && mp[string(s[i+1])] > cur {
			cur = -cur
		}
		res += cur
	}
	return res
}

func getRomanMap() map[string]int {
	mp := make(map[string]int)
	mp["I"] = 1
	mp["V"] = 5
	mp["X"] = 10
	mp["L"] = 50
	mp["C"] = 100
	mp["D"] = 500
	mp["M"] = 1000
	return mp
}

// 基本思路：MCMXCIV = 1000 + 100 + 1000 - 100 + 10 + 5 - 1 = 1994
// 规律：如果当前数字小于下一个数字，那么当前数字取反
func main() {
	s := "MCMXCIV"
	fmt.Println(romanToInt(s))
}
